﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using QuestionsAnswers.UI.Models;

namespace QuestionsAnswers.UI.Controllers
{ 
    public class AnswerController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();

        public ActionResult Index(int id)
        {
            var answers = _db.Answers.Include(a => a.User).Include(a => a.Question).Where(a=>a.QuestionId ==id);

            ViewBag.Question = _db.Questions.Find(id);

            return View(answers.ToList());
        }

        [Authorize]
        public ActionResult Create(int id)
        {
           
            Question question = _db.Questions.Find(id);
            string user = User.Identity.Name;
            User userMem = _db.Users.First(x => x.UserName == user);
           
            Answer answer = new Answer { QuestionId = question.QuestionId, UserId = userMem.UserId ,User=userMem,Question = question};
            return Request.IsAjaxRequest()?(ActionResult) PartialView(answer):View(answer);
        } 

        [Authorize]
        [HttpPost]
        public ActionResult Create(Answer answer)
        {
            if (ModelState.IsValid)
            {
                if (!_db.Answers.Any(a => a.QuestionId == answer.QuestionId && a.User.UserName == User.Identity.Name))
                {
                    _db.Answers.Add(answer);
                    _db.SaveChanges();
                }
                return RedirectToAction("Index",new {id= answer.QuestionId});  
            }

            return View(answer);
        }
        
 
        public ActionResult Edit(int id)
        {
            Answer answer = _db.Answers.Include("User").Include("Question").FirstOrDefault(x => x.AnswerId ==id);

            return Request.IsAjaxRequest() ? (ActionResult)PartialView(answer) : View(answer);
        }


        [HttpPost]
        public ActionResult Edit(Answer answer)
        {
            if (ModelState.IsValid&& User.Identity.Name ==_db.Users.First(u=>u.UserId==answer.UserId).UserName)
            {
                _db.Entry(answer).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index",new {id = answer.QuestionId});
            }

            return View(answer);
        }

 
        public ActionResult Delete(int id)
        {
            Answer answer =
                _db.Answers.Include(a => a.User).Include(b => b.Question).FirstOrDefault(c => c.AnswerId == id);
            return View(answer);
        }


        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Answer answer = _db.Answers.Include("User").First(a=>a.AnswerId==id);

            if (User.Identity.Name == answer.User.UserName)
            {
                _db.Answers.Remove(answer);
                _db.SaveChanges();
            }
            return RedirectToAction("Index",new {id= answer.QuestionId});
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}